package com.cl.blog.web.manager;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cl.blog.annotation.Login;
import com.cl.blog.common.Const;
import com.cl.blog.common.Result;
import com.cl.blog.entity.BType;
import com.cl.blog.service.BTypeService;
import com.cl.blog.vo.BatchType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 付
 * @Date 2024-03-18 13:09
 * @Version 1.0
 * @description
 * @since 1.0
 */
@RestController
@RequestMapping("/admin/type")
public class AdminTypeController {

	@Resource
	BTypeService bTypeService;

	/**
	 * 查所有，且根据条件查询
	 *
	 * @return
	 */
	@GetMapping("/list")
	@Login(issuer = Const.GRADE_ADMIN)
	public Result list(Integer id) {
		List<BType> bTypes = bTypeService.list(new QueryWrapper<BType>()
				.eq(id != null, "id", id)
		);

		Assert.notNull(bTypes, "抱歉，数据获取失败，稍后再试");
		return Result.success(bTypes);
	}

	// 单个修改
	@GetMapping("/update")
	@Login(issuer = Const.GRADE_ADMIN)
	public Result updateById(Integer id, String typeName) {
		Assert.notNull(id, "都没有选择，你删个毛啊");
		Assert.notNull(typeName, "抱歉，不知道你修改的是什么");
		boolean i = bTypeService.updateType(id, typeName);

		return Result.success(i);
	}

	// 单个删除
	@GetMapping("/delete")
	@Login(issuer = Const.GRADE_ADMIN)
	public Result delete(Integer id) {
		Assert.notNull(id, "都没有选择，你删个毛啊");
		return Result.success(bTypeService.removeById(id));
	}

	@PostMapping("/batch-update")
	@Login(issuer = Const.GRADE_ADMIN)
	public Result batchUpdate(@Validated @RequestBody BatchType batchType) {
		System.out.println("修改的数组---" + batchType.getIds());
		System.out.println("修改的数组name---" + batchType.getNames());
//		Assert.isTrue(batchType.getIds().size()!=batchType.getNames().size(),"抱歉，修改格式错误，可能是数量不对等");
		if (batchType.getIds().size() != batchType.getNames().size()) {
			return Result.error("抱歉，修改格式错误，可能是数量不对等");
		}
		return Result.success(bTypeService.updateBatchType(batchType.getIds(), batchType.getNames()));
	}

	@PostMapping("/batch-delete")
	@Login(issuer = Const.GRADE_ADMIN)
	public Result batchDelete(@RequestBody List<Integer> ids) {
		System.out.println("删除的数组---" + ids);
		return Result.success(bTypeService.removeBatchByIds(ids));
	}

	@GetMapping("/add")
	@Login(issuer = Const.GRADE_ADMIN)
	public Result addType(String names) {
		System.out.println("获取的数据----" + names);

		return Result.success(bTypeService.saveName(names));
	}
}
